package com.xcf.weekly_2020_200;

import java.util.HashSet;
import java.util.Set;

public class CalThreeTriple {

    /**
     * 0 <= i < j < k < arr.length
     * |arr[i] - arr[j]| <= a
     * |arr[j] - arr[k]| <= b
     * |arr[i] - arr[k]| <= c
     *
     * @param arr
     * @param a
     * @param b
     * @param c
     * @return
     */
    public int countGoodTriplets(int[] arr, int a, int b, int c) {
        int result = 0;
        Set<String> set = new HashSet<>();
        for (int i = 0; i < arr.length - 2; i++) {
            for (int j = i + 1; j < arr.length - 1; j++) {
                for (int k = j + 1; k < arr.length; k++) {
                    if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
                        String index = i + "_" + j + "_" + k;
                        if (set.add(index)) {
                            result++;
                        }
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        CalThreeTriple triple = new CalThreeTriple();
        int result = triple.countGoodTriplets(new int[]{1,1,2,2,3}, 0, 0, 1);
        System.out.println("result = " + result);
    }
}
